package com.quwan.utils;

import com.quwan.common.constant.OssConstant;
import com.quwan.common.exception.QuWanException;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author lt
 * @Date 2021/12/1 12:30 下午
 */
public class FIleCheckUtils {

    public final static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

    public static File fileCheck(String type, MultipartFile file) throws IOException {
        File result = null;
        // 用户相关的，头像，证件照
        if (type.equals(OssConstant.USER_IMG)) {
            result = checkUserImg(file);
        }

        return result;
    }

    /**
     * 校验头像证件照图片的大小和尺寸
     *
     * @param multipartFile
     * @throws IOException
     */
    public static File checkUserImg(MultipartFile multipartFile){
        File result = null;
        // 获取图片大小
        long length = multipartFile.getSize();
        if (length > 5242880) {
            throw new QuWanException("图片大小不能超过5M");
        }
//        if (length > (1024 * 100)) {
//            result = ImageUtils.compressPicForScale(multipartFile.getInputStream(), (1024 * 100), multipartFile.getSize(), "publicservice");
//        }
        return result;
    }

    /**
     * 使用uuid创建文件名称
     *
     * @param suffix
     * @return
     */

    public static String getUUIDName(String suffix) {
        return new StringBuffer().append(UUID.randomUUID().toString()).append(dateFormat.format(new Date()))
                .append(suffix).toString();
    }
}
